﻿
using Xacml.Core;
using System;
using System.Xml;

namespace Xacml.Core.Policy
{
  public abstract class TargetItemsBaseReadWrite : XacmlElement
  {
    private TargetItemCollectionReadWrite _items = new TargetItemCollectionReadWrite();
    private bool _anyItem;

    public bool IsAny
    {
      get
      {
        return this._anyItem;
      }
      set
      {
        this._anyItem = value;
      }
    }

    public virtual TargetItemCollectionReadWrite ItemsList
    {
      get
      {
        return this._items;
      }
      set
      {
        this._items = value;
      }
    }

    protected TargetItemsBaseReadWrite(XacmlVersion schemaVersion)
      : base(XacmlSchema.Policy, schemaVersion)
    {
    }

    protected TargetItemsBaseReadWrite(bool anyItem, TargetItemCollectionReadWrite items, XacmlVersion schemaVersion)
      : this(schemaVersion)
    {
      this._anyItem = anyItem;
      this._items = items;
    }

    protected TargetItemsBaseReadWrite(XmlReader reader, string itemsNodeName, string anyItemNodeName, string itemNodeName, XacmlVersion schemaVersion)
      : this(schemaVersion)
    {
      if (reader.LocalName == itemsNodeName && this.ValidateSchema(reader, schemaVersion))
      {
        while (reader.Read())
        {
          if (reader.LocalName == anyItemNodeName && this.ValidateSchema(reader, schemaVersion))
            this._anyItem = true;
          else if (reader.LocalName == itemNodeName && this.ValidateSchema(reader, schemaVersion))
            this._items.Add(this.CreateTargetItem(reader));
          else if (reader.LocalName == itemsNodeName && reader.NodeType == XmlNodeType.EndElement)
            break;
        }
      }
      else
        throw new Exception(Resource.ResourceManager[Resource.MessageKey.exc_invalid_node_name, new object[1]
        {
          (object) reader.LocalName
        }]);
    }

    protected abstract TargetItemBaseReadWrite CreateTargetItem(XmlReader reader);
  }
}
